package com.study.tomcat.v3;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: chenhs
 * @date: Created in 17:17 2019/12/21
 **/
public class WebXmlConfigUtil extends DefaultHandler {

    /**
     * Servlet集合
     */
    Map<String, Object> servlets = new HashMap<>();


    /**
     * Servlet映射
     */
    Map<String, String> servletMapping = new HashMap<>();


    /**
     * xml文件地址
     */
    private String xmlPath;


    public ProjectUtil.WebXml loadXml(String xmlPath) throws Exception {
        this.xmlPath = xmlPath;
        //创建一个解析XML的工厂对象
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        //创建一个解析XML的对象
        SAXParser parser = parserFactory.newSAXParser();
        //创建一个解析助手类
        parser.parse(this.xmlPath, this);

        ProjectUtil.WebXml webXml = new ProjectUtil().new WebXml();
        webXml.servlets = this.servlets;
        webXml.servletMapping = this.servletMapping;
        return webXml;
    }

    String currentServlet = null;
    String currentServletMapping = null;
    String currentParam = null;
    String qName = null;


    /**
     * 开始解析文档，即开始解析XML根元素时调用该方法
     */
    @Override
    public void startDocument() throws SAXException {
        System.out.println("开始解析：" + this.xmlPath);
    }

    /**
     * 开始解析每个元素时都会调用该方法
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //判断正在解析的元素是不是开始解析的元素
        this.qName = qName;
    }

    /**
     * 解析到每个元素的内容时会调用此方法
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String currentValue = new String(ch, start, length);
        if (currentValue == null || "".equals(currentValue.trim()) || "\n".equals(currentValue.trim())) {
            return;
        }

        if ("servlet-name".equals(qName)) {
            currentServlet = currentValue;
            currentServletMapping = currentValue;
        } else if ("servlet-class".equals(qName)) {
            // servlet信息
            String servletClass = currentValue;
            servlets.put(currentServlet, servletClass);
        } else if ("param-name".equals(qName)) {
            currentParam = currentValue;
        } else if ("param-value".equals(qName)) {
            String paramValue = currentValue;
            //servlet param 参数
            HashMap<String, String> params = new HashMap<>();
            params.put(currentParam, paramValue);
        } else if ("servlet-name".equals(qName)) {
            currentServletMapping = currentValue;
        } else if ("url-pattern".equals(qName)) {
            String urlPattern = currentValue;
            // url映射
            servletMapping.put(urlPattern, currentServletMapping);
        }
    }

    /**
     * 结束解析每个元素时都会调用该方法
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
    }

    /**
     * 结束解析文档，即解析根元素结束标签时调用该方法
     */
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    public Map<String, Object> getServlets() {
        return servlets;
    }

    public void setServlets(Map<String, Object> servlets) {
        this.servlets = servlets;
    }

    public Map<String, String> getServletMapping() {
        return servletMapping;
    }

    public void setServletMapping(Map<String, String> servletMapping) {
        this.servletMapping = servletMapping;
    }

    public String getXmlPath() {
        return xmlPath;
    }

    public void setXmlPath(String xmlPath) {
        this.xmlPath = xmlPath;
    }
}
